<?php

namespace App\Http\Model;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class Relation extends Base
{
    protected $table = 'relation_rank';
    protected $userRelationTable = 'user_relation';//用户亲密关系
    protected $userRelationLogTable = 'user_relation_log';//用户亲密关系记录

    //亲密关系等级缓存
    protected $relationRankKey = 'RELATIONRANK:CONFIG';//string RELATIONRANK:CONFIG => {string}

    public function __construct(array $attributes = [])
    {
        parent::__construct($this->table, $attributes);
    }

    /**
     * 获取许愿转盘展示礼物
     */
    public function getRelationRank(){
        $key = $this->relationRankKey;
        $list = Redis::get($key);
        if(empty($list)){
            $list = DB::table($this->table)->get();
            $list = self::objToArray($list);
            if(!empty($list)) {
                Redis::setex($key, 86400, json_encode($list));
            }
        }else{
            $list = json_decode($list, true);
        }
        foreach($list as $key=>$val) {
            $val['image'] = build_url($val['image']);
            $list[$key] = $val;
        }

        return $list;
    }

    //新增亲密关系用户记录
    public function addUserRelationLog($data)
    {
        $now = time();
        $id = DB::table($this->userRelationLogTable)->insertGetId($data);

        $info = DB::table($this->userRelationTable)
            ->where('user_id_1', $data['user_id_1'])
            ->where('user_id_2', $data['user_id_2'])
            ->where('rank_id', $data['rank_id'])
            ->first();
        $info =  objToArray($info);
        if(empty($info)) {
            $log = [
                'user_id_1' => $data['user_id_1'],
                'user_id_2' => $data['user_id_2'],
                'rank_id' => $data['rank_id'],
                'start_time' => $now,
                'end_time' => $now + 86400*$data['day'],
            ];
            $id = DB::table($this->userRelationTable)->insertGetId($log);
        }else{
            //已过期
            if($info['end_time'] < $now) {
                DB::table($this->userRelationTable)
                    ->where('id', $info['id'])
                    ->update(['start_time' => $now, 'end_time' => $now + 86400*$data['day']]);
            }else{
                DB::table($this->userRelationTable)->where('id', $info['id'])->increment('end_time', (86400*$data['day']));
            }
        }

        return $id;
    }

    //获取用户亲密关系列表
    public function getUserRelationAll($userId)
    {
        $list = DB::table($this->userRelationTable)
            ->where('end_time', '>', time())
            ->where(function ($query) use ($userId) {
                $query->where('user_id_1', $userId)
                    ->orWhere('user_id_2', $userId);
            })
            ->orderBy('updated_at', 'desc')
            ->get();
        $list = objToArray($list);

        return $list;
    }

    //获取用户亲密关系列表根据结束时间范围
    public function getUserRelationByEndTime($timeSt, $timeEd)
    {
        $list = DB::table($this->userRelationTable)
            ->where('end_time', '>=', $timeSt)
            ->where('end_time', '<', $timeEd)
            ->get();
        $list = objToArray($list);

        return $list;
    }

}
